# -*- coding: utf-8 -*-
# @Time : 2021/9/17 16:07
# @Author : Mr  jelis
# @Email : 1402615849@qq.com
# @File : shopping_page.py
# @Project : mryx_test
"""购物车页面类"""
from page.base_page import BasePage
from appium.webdriver.webdriver import MobileBy as By
from appium.webdriver.webdriver import MobileBy
from time import sleep
class Shopping(BasePage):
    """定义定位器"""
    delete_locator = (By.XPATH, '//android.view.View[@content-desc=\"删除\"]')
    sure_locator = (By.XPATH, '//android.widget.TextView[@resource-id=\"cn.missfresh.application:id/tv_ensure\"]')
    add_number_locator = (By.XPATH, '//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/'
              'android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/'
              'android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/'
              'android.view.View[2]/android.widget.ScrollView[1]/android.view.View[2]/android.widget.ImageView[5]')
    settlement_locator=(By.XPATH,'//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/'
                             'android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/'
                                 'android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/'
                                 'android.view.View[1]/android.view.View[7]')

    goods1_locator = (MobileBy.XPATH,
                      '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout'
                      '/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.'
                      'widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup'
                      '/androidx.recyclerview.widget.RecyclerView/android.widget.LinearLayout[3]/android.widget.'
                      'RelativeLayout/android.widget.TextView[1]')   #购物车页面最新商品的名称定位器

    delete_locator2 = (MobileBy.ID, 'cn.missfresh.application:id/tv_delete')  #【删除】定位器
    ensure_locator = (MobileBy.ID, 'cn.missfresh.application:id/tv_ensure')  # 弹窗的【确定】按钮


    text_locator = (MobileBy.XPATH,
                    '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                    'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.'
                    'FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx.'
                    'recyclerview.widget.RecyclerView/android.widget.LinearLayout[2]/android.widget.TextView')# 获取购物车清空提示

    number_locator = (MobileBy.ID, 'cn.missfresh.application:id/cartNumTv')  #获取购物车的商品总数

    price1_locator = (
    MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("cn.missfresh.application:id/pstv_left_price")')
       #第一个商品的价格
    price2_locator = (MobileBy.XPATH,
                      '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                      'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.'
                      'FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx'
                      '.recyclerview.widget.RecyclerView/android.widget.LinearLayout[4]/android.widget.RelativeLayout/'
                      'android.widget.TextView[3]')  #第二个商品的价格
    price3_locator = (MobileBy.XPATH,
                      '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/'
                      'android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.'
                      'FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/androidx'
                      '.recyclerview.widget.RecyclerView/android.widget.LinearLayout[5]/android.widget.RelativeLayout/'
                      'android.widget.TextView[3]')   #第三个商品的价格

    total_price_locator = (MobileBy.XPATH,
                           '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.'
                           'FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.'
                           'FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.'
                           'LinearLayout/android.widget.RelativeLayout/android.widget.TextView[4]')   #购物车结算的金额
    shangping_name_locator = (By.XPATH,'//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.ScrollView[1]/android.view.View[2]')
    size_locator=(By.XPATH,'//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[2]/android.widget.ScrollView[1]/android.widget.ImageView[1]')
    shangping_num_locator=(By.XPATH,'//android.widget.EditText[@text=\"3\"]')
    total_price_locator = (MobileBy.ID,'cn.missfresh.application:id/tv_price')   #购物车结算的金额

    discount_goods_locator = (MobileBy.XPATH,'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/'
                                             'android.widget.FrameLayout/android.widget.LinearLayout/android.widget.'
                                             'FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android'
                                             '.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/'
                                             'androidx.recyclerview.widget.RecyclerView/android.widget.RelativeLayout'
                                             '[3]/android.widget.ImageView[2]')   #购物车[人选2件8折] 商品定位器

    address_locator = (By.XPATH, '//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/'
              'android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/'
              'android.view.View[1]/android.view.View[1]/android.view.View[1]/android.view.View[1]/'
              'android.view.View[2]/android.widget.ScrollView[1]/android.view.View[1]') #结算页面的默认地址
    select_address_locator=(By.XPATH,'//android.view.View/android.view.View[2]/android.view.View[1]/android.view.View[1]/android.widget.ImageView[1]')

    def delete2(self):
        """删除购物车商品"""
        self.driver.find_element(*self.delete_locator2).click()
    def ensure(self):
        """确定删除"""
        self.driver.find_element(*self.ensure_locator).click()

    def delete(self):
        """删除购物车商品"""
        self.driver.find_element(*self.delete_locator).click()
    def sure(self):
        """确定删除"""
        self.driver.find_element(*self.sure_locator).click()

    def add_number(self,num=2):
        """修改购物车数量"""
        for i in range(num):
            self.driver.find_element(*self.add_number_locator).click()
            sleep(2)
    def click_settlement(self):
        """点击结算"""
        self.driver.find_element(*self.settlement_locator).click()

    def get_first_name(self):
        '''获取购物车页面最新商品名称'''
        text = self.driver.find_element(*self.goods1_locator).text   #获取购物车最新的商品名，做断言用
        return text
    def shangping_name(self):
        '''获取购物车页面商品名称'''
        text = self.driver.find_element(*self.shangping_name_locator).get_attribute("content-desc")   #获取购物车商品名，做断言用
        return text

    def get_message(self):
        '''获取清空购物车的提示语'''
        text = self.driver.find_element(*self.text_locator).text
        return text

    def get_goods_number(self):
        '''获取购物车里商品总数量'''
        number = self.driver.find_element(*self.number_locator).text
        return number

    def click_firgoods(self):
        '''点击购物车最新商品名字'''
        self.driver.find_element(*self.goods1_locator).click()

    def get_price1(self):
        '''获取第一个商品价格'''
        price1 = self.driver.find_element(*self.price1_locator).text
        return price1

    def get_price2(self):
        '''获取第一个商品价格'''
        price2 = self.driver.find_element(*self.price2_locator).text
        return price2

    def get_price3(self):
        '''获取第一个商品价格'''
        price3 = self.driver.find_element(*self.price3_locator).text
        return price3

    def get_total_price(self):
        '''获取总计商品金额'''
        sleep(3)
        total_price = self.driver.find_element(*self.total_price_locator).text
        return total_price
    def size(self):
        """获取删除购物车后元素的大小"""
        size=self.driver.find_element(*self.size_locator).size
        return size
    def shangping_num(self):
        """获取商品数量"""
        num=self.driver.find_element(*self.shangping_num_locator).get_attribute("text")
        return num
    def add_discount_goods(self):
        '''添加任选两件八折商品'''
        self.driver.find_element(*self.discount_goods_locator).click()


    def frist_shop(self,shop=(170,870)):
        '''点击购物车页面的第一个商品，进入详情页面'''
        self.driver.tap([shop])

    def click_address(self):
        '''点击收货地址'''
        self.driver.find_element(*self.address_locator).click()

    def address(self):
        '''获取结算页面的默认地址信息'''
        return self.driver.find_element(*self.address_locator).get_attribute("content-desc")

    def back(self,b=(34,64)):
        '''返回上一页'''
        self.driver.tap([b])

    def select_address(self):
        '''选择收货地址'''
        self.driver.find_element(*self.select_address_locator).click()
